<template>
  <div>
    <el-dialog
      :title="dialogInfo.isAdd ? '添加角色' : '编辑角色'"
      :visible.sync="dialogInfo.isShow"
      center
      :before-close="cancel"
    >
      <el-form :model="roleForm" :rules="rules" ref="roleInfo">
        <el-form-item
          label="角色名称"
          :label-width="formLabelWidth"
          prop="rolename"
        >
          <el-input v-model="roleForm.rolename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色权限" :label-width="formLabelWidth">
          <el-tree
            :data="meunList"
            show-checkbox
            node-key="id"
            default-expand-all
            ref="tree"
            :props="{
              children: 'children',
              label: 'title', //名字
            }"
          >
          </el-tree>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="roleForm.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
        <!-- <el-button type="primary" v-if="dialogInfo.isAdd" @click="addInfo">添加</el-button>
        <el-button type="primary" v-else @click="aditInfo">编辑</el-button> -->
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addRole, getRoleInfo, postEditRole } from "@/util/axios";
import { mapActions, mapGetters } from "vuex";
export default {
  data() {
    return {
      roleForm: {
        rolename: "", //角色名称
        menus: "", //角色权限//菜单权限 存放的是菜单编号，用逗号隔开 '[1,2,3]'
        status: 1, //状态1正常2禁用
      },
      formLabelWidth: "120px",
      rules: {
        rolename: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    ...mapGetters({
      meunList: "menu/getMenuList",
    }),
  },
  props: ["dialogInfo"],
  mounted() {
    if(this.meunList.length==0){
      this.getMenuList()
    }
  },
  methods: {
    ...mapActions({
      getRoleList: "role/getRoleListAction",
      getMenuList: "menu/getMenuListAction",
    }),
    //封装一个取消事件
    cancel() {
      this.$emit("cancel", false);
      //调用封装好的重置事件
      this.reset();
    },
    //封装一个重置事件
    reset() {
      (this.roleForm = {
        rolename: "", //角色名称
        menus: "", //角色权限//菜单权限 存放的是菜单编号，用逗号隔开 '[1,2,3]'
        status: 1, //状态1正常2禁用
      }),
        //重置规则验证
        this.$refs.roleInfo.resetFields();
      //重置tree结构
      this.$refs.tree.setCheckedKeys([]);
    },
    //封装一个获取一条数据的方法
    lookup(id) {
      getRoleInfo({ id }).then((res) => {
        if (res.data.code == 200) {
          this.roleForm = res.data.list;
          //重新给tree结构赋值
          this.$refs.tree.setCheckedKeys(this.roleForm.menus.split(","));
          this.roleForm.id = id;
        }
      });
    },
    confirm() {
      this.$refs.roleInfo.validate((valid) => {
        if (valid) {
          //根据后端需求，它需要的参数叫 menus。要求是'1,2,3'这种状态,我们要根据后端需求,对我们的数据进行格式转化
          this.roleForm.menus = this.$refs.tree.getCheckedKeys().join(",");
          if (this.dialogInfo.isAdd) {
            //封装一个添加接口
            addRole(this.roleForm).then((res) => {
              if (res.data.code == 200) {
                this.$message.success(res.data.msg);
                //当添加成功要清空弹框并重新调取列表
                this.cancel();
                this.getRoleList();
              } else {
                this.$message.error(res.data.msg);
              }
            });
          } else {
            //编辑事件
            postEditRole(this.roleForm).then((res) => {
              if (res.data.code == 200) {
                this.$message.success(res.data.msg);
                //当添加成功要清空弹框并重新调取列表
                this.cancel();
                this.getRoleList();
              } else {
                this.$message.error(res.data.msg);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style lang="" scoped>
</style>